package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.annotation.CurrentUser;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardItem;
import com.liugh.service.zhongche.IStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "标准条目模块")
@RestController
@RequestMapping("/item")
public class StandardItemController extends BaseRestController {
    @Autowired
    private IStandardService standardService;

    @ApiOperation(value = "添加标准条目",notes = "category，standAttach，standName，standNo，uploadDir必传")
//    @Log(action = "添加标准条目",modelName = "standardItem")
    @PostMapping("/add")
    public ResponseModel<StandardItem> addStandItem(@RequestBody StandardItem standardItem,
                                                        @CurrentUser User currentUser) throws BusinessException {
        if (standardService.add(standardItem,currentUser) == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(standardItem);
    }

    @ApiOperation(value = "更新标准条目",notes = "id,category，standAttach，standName，standNo，uploadDir必传")
//    @Log(action = "更新标准条目",modelName = "standardItem")
    @PutMapping("/update")
    public ResponseModel<StandardItem> updateItem(@RequestBody StandardItem standardItem,
                                                  @CurrentUser User user) throws BusinessException {
        if (standardService.update(standardItem,user) == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(standardItem);
    }


    @ApiOperation("通过ID获取条目")
    @GetMapping("/id")
    public ResponseModel<StandardItem> getItemById(@RequestParam(value = "id",required = true)int id) {
        return fillSuccessData(standardService.getItemById(id));
    }


    @ApiOperation("分页获取所有的条目")
    @GetMapping("/all_list")
    public ResponseModel<Page<StandardItem>> getAllItemPage(@RequestParam(value = "pageNum",defaultValue = "1",required = false)int pageNum,
                                                            @RequestParam(value = "pageSize",defaultValue = "10",required = false)int pageSize) {

        return fillSuccessDataPage(standardService.getAllItemPage(new Page<>(pageNum, pageSize)));
    }

    //返回我上传的条目
    @ApiOperation("分页获取我上传的文件")
    @GetMapping("/my_item")
    public ResponseModel<Page<StandardItem>> getMyItem(@RequestParam(value = "pageNum",defaultValue = "1",required = false)int pageNum,
                                                       @RequestParam(value = "pageSize",defaultValue = "10",required = false)int pageSize,
                                                       @CurrentUser User user) {
        Page<StandardItem> itemPage = standardService.getMyUploadItem(new Page<>(pageNum,pageSize),user.getUserNo());
       return fillSuccessDataPage(itemPage);
    }
}
